import { userlist, usercount } from '../../../request/api';

interface sta {
    userList: Array<any>,
    userCount: number,
    pageData: Object
}

let state: sta = {
    userList: [],
    userCount: 1,
    pageData: {
        page: 1,
        size: 2
    }
}

export default {
    namespaced: true,
    state,
    getters: {

    },
    mutations: {
        changeUserList(state: any, data: any) {
            state.userList = data;
        },
        changeUserCount(state: any, data: any) {
            state.userCount = data;
        },
        changePage(state: any, data: any) {
            state.pageData.page = data;
        }
    },
    actions: {
        getUserList({ dispatch, state, commit }: any) {
            userlist(state.pageData).then((res: any) => {
                if (res.code == 200) {
                    let list = res.list ? res.list : [];
                    commit('changeUserList', list);
                }
            })
        },
        getUserCount({ commit, state, dispatch }: any, tag: any) {
            usercount().then((res: any) => {
                if (res.code == 200) {
                    commit('changeUserCount', res.list[0].total);
                    if (tag) {
                        commit('changePage', Math.ceil(state.userCount / state.pageData.size));
                        dispatch('getUserList');
                    }
                }
            })
        },
    },
    modules: {
    }
}
